package org.iblink.gui.swing.actions;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.AbstractAction;

/**
 * Generic action upon assets.
 * 
 * @author tpares
 *
 */
public class AssetAction extends AbstractAction {
  
  public static final String ADDACTION = "ADD";
  public static final String DELETEACTION = "DELETE";
  public static final String OPENACTION = "OPEN";
  public static final String EDITACTION = "EDIT";
  
  private static AssetAction addAssetAction;
  private static AssetAction delAssetAction;
  private static AssetAction openAssetAction;
  private static AssetAction editAssetAction;

  /**
   * Generated serial version UID
   */
  private static final long serialVersionUID = 9033487030215044703L;
  private String command;
  
  private List<ActionListener> listeners = new ArrayList<ActionListener>();
  
  private AssetAction(String text, String command) {
    super(text);
    
    this.command = command;
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    ActionEvent event = new ActionEvent(e.getSource(), e.getID(), command);
    for (Iterator<ActionListener> iter = listeners.iterator(); iter.hasNext();) {
      iter.next().actionPerformed(event);
    }
  }
  
  public void addActionListener(ActionListener listener) {
    if (!listeners.contains(listener)) {
      listeners.add(listener);
    }
  }
  
  public void removeActionListener(ActionListener listener) {
    if (listeners.contains(listener)) {
      listeners.remove(listener);
    }
  }
  
  public static AssetAction getAddAssetAction() {
    if (addAssetAction == null) {
      addAssetAction = new AssetAction("Add", AssetAction.ADDACTION);
      addAssetAction.setEnabled(false);
    }
    
    return addAssetAction;
  }
  
  public static AssetAction getDelAssetAction() {
    if (delAssetAction == null) {
      delAssetAction = new AssetAction("Delete", AssetAction.DELETEACTION);
      delAssetAction.setEnabled(false);
    }
    
    return delAssetAction;
  }
  
  public static AssetAction getOpenAssetAction() {
    if (openAssetAction == null) {
      openAssetAction = new AssetAction("Open", AssetAction.OPENACTION);
      openAssetAction.setEnabled(false);
    }
    
    return openAssetAction;
  }
  
  public static AssetAction getEditAssetAction() {
    if (editAssetAction == null) {
      editAssetAction = new AssetAction("Edit", AssetAction.EDITACTION);
      editAssetAction.setEnabled(false);
    }
    
    return editAssetAction;
  }
}
